﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
namespace ToaThuoc_PAK.Handler
{
    /// <summary>
    /// Summary description for XML_DanhSachBenhNhanCho
    /// </summary>
    public class XML_DanhSachBenhNhanCho : IHttpHandler
    {
        private string TuNgay;
        private string DenNgay;
        private string MaKhoa;
        private string MaBN;
        private string HoTen;
        private DAL_BenhNhan dal_BenhNhan;
        public void ProcessRequest(HttpContext context)
        {
            this.TuNgay = Factory.emptyNull(context.Request.QueryString["TuNgay"]);
            this.DenNgay = Factory.emptyNull(context.Request.QueryString["DenNgay"]);
            this.MaKhoa = Factory.emptyNull(context.Request.QueryString["MaKhoa"]);
            this.MaBN = Factory.emptyNull(context.Request.QueryString["MaBN"]);
            this.HoTen = Factory.emptyNull(context.Request.QueryString["HoTen"]);
            if (this.TuNgay == "")
            {
                this.TuNgay = DateTime.Today.ToString("dd/MM/yyyy");
            }
            if (this.DenNgay == "")
            {
                this.DenNgay = DateTime.Today.ToString("dd/MM/yyyy");
            }

            string strXML = "";
            strXML = LoadData();
            context.Response.ContentType = "text/xml";
            context.Response.Write(strXML);
            context.Response.End();
        }
        private string LoadData()
        {
            string strXML = "<Root>" + "\n";
            dal_BenhNhan = new DAL_BenhNhan();
            DataTable dt = dal_BenhNhan.LayDanhSachChoKham(this.MaKhoa, this.HoTen, this.MaBN, this.TuNgay, this.DenNgay);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                string strNgay = "";
                if (Factory.IsDate(dr["Ngay"]))
                {
                    strNgay = Convert.ToDateTime(dr["Ngay"]).ToString("dd/MM/yyyy HH:MM");
                }

                strXML += "<DanhSachBenhNhanCho  STT=\"" + Factory.emptyNull(dr["STT"]) + "\"" + "\n" +
                          "                      ID=\"" + Factory.emptyNull(dr["ID"]) + "\"" + "\n" +
                          "                      Ngay=\"" + strNgay + "\"" + "\n" +
                          "                      MaBN=\"" + Factory.emptyNull(dr["MaBN"]) + "\"" + "\n" +
                          "                      HoTen=\"" + Factory.emptyNull(dr["HoTen"]) + "\"" + "\n" +
                          "                      Tuoi=\"" + Factory.emptyNull(dr["Tuoi"]) + "\"" + "\n" +
                          "                      UuTien=\"" + Factory.emptyNull(dr["UuTien"]) + "\"" + "\n" +
                          "                      DaKeToa=\"" + Factory.emptyNull(dr["DaKeToa"]) + "\"" + "\n" +
                          "                      Loaikham=\"" + Factory.emptyNull(dr["Loaikham"]) + "\"" + ">" + "\n" +
                          "</DanhSachBenhNhanCho>" + "\n";
            }
            strXML += "</Root>";
            return strXML;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}